<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>Mybatis快速入门五(关联查询映射) | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="SSM">
    <meta name="description" content="分析数据模型之前的博客里有提到过使用Mybatis对数据库单表进行映射，执行增删改查操作。但是在现实的项目中进行数据库建模时，我们要遵循数据库设计范式的要求，会对现实中的业务模型进行拆分，封装在不同的数据表中，表与表之间存在着一对多或是多对多的对应关系。 分析思路 1 明确每张表存储的信息 2 明确每张表中的关键字段(主键，外键，非空) 3 明确数据库中表与表之间的外键关系 4 明确业务中表与表的">
<meta name="keywords" content="SSM">
<meta property="og:type" content="article">
<meta property="og:title" content="Mybatis快速入门五(关联查询映射)">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="分析数据模型之前的博客里有提到过使用Mybatis对数据库单表进行映射，执行增删改查操作。但是在现实的项目中进行数据库建模时，我们要遵循数据库设计范式的要求，会对现实中的业务模型进行拆分，封装在不同的数据表中，表与表之间存在着一对多或是多对多的对应关系。 分析思路 1 明确每张表存储的信息 2 明确每张表中的关键字段(主键，外键，非空) 3 明确数据库中表与表之间的外键关系 4 明确业务中表与表的">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/%E5%85%B3%E7%B3%BB%E5%9B%BE.png">
<meta property="og:updated_time" content="2017-12-05T10:18:16.496Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mybatis快速入门五(关联查询映射)">
<meta name="twitter:description" content="分析数据模型之前的博客里有提到过使用Mybatis对数据库单表进行映射，执行增删改查操作。但是在现实的项目中进行数据库建模时，我们要遵循数据库设计范式的要求，会对现实中的业务模型进行拆分，封装在不同的数据表中，表与表之间存在着一对多或是多对多的对应关系。 分析思路 1 明确每张表存储的信息 2 明确每张表中的关键字段(主键，外键，非空) 3 明确数据库中表与表之间的外键关系 4 明确业务中表与表的">
<meta name="twitter:image" content="http://om6odecqr.bkt.clouddn.com/%E5%85%B3%E7%B3%BB%E5%9B%BE.png">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Mybatis快速入门五(关联查询映射)</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Mybatis快速入门五(关联查询映射)</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-08-14T11:10:00.000Z" itemprop="datePublished" class="page-time">
  2017-08-14
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#分析数据模型"><span class="post-toc-number">1.</span> <span class="post-toc-text">分析数据模型</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#分析思路"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">分析思路</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#模型分析"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">模型分析</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#创建PO类"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">创建PO类</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#一对一查询"><span class="post-toc-number">2.</span> <span class="post-toc-text">一对一查询</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#需求"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">需求</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#使用resultType完成功能"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">使用resultType完成功能</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#使用resultMap"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">使用resultMap</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#一对一小结"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">一对一小结</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#一对多查询"><span class="post-toc-number">3.</span> <span class="post-toc-text">一对多查询</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#需求-1"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">需求</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#使用resultMap-1"><span class="post-toc-number">3.2.</span> <span class="post-toc-text">使用resultMap</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#多对多查询"><span class="post-toc-number">4.</span> <span class="post-toc-text">多对多查询</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#需求-2"><span class="post-toc-number">4.1.</span> <span class="post-toc-text">需求</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#延迟加载"><span class="post-toc-number">5.</span> <span class="post-toc-text">延迟加载</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#如何设置延迟加载"><span class="post-toc-number">5.1.</span> <span class="post-toc-text">如何设置延迟加载</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#使用association进行延迟加载"><span class="post-toc-number">5.2.</span> <span class="post-toc-text">使用association进行延迟加载</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-Mybatis_primary_05"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Mybatis快速入门五(关联查询映射)</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年08月14日 19:10" datetime="2017-08-14T11:10:00.000Z"  itemprop="datePublished">2017-08-14</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="分析数据模型"><a href="#分析数据模型" class="headerlink" title="分析数据模型"></a>分析数据模型</h2><p>之前的博客里有提到过使用Mybatis对数据库单表进行映射，执行增删改查操作。但是在现实的项目中进行数据库建模时，我们要遵循数据库设计范式的要求，会对现实中的业务模型进行拆分，封装在不同的数据表中，表与表之间存在着一对多或是多对多的对应关系。</p>
<h3 id="分析思路"><a href="#分析思路" class="headerlink" title="分析思路"></a>分析思路</h3><ul>
<li>1 明确每张表存储的信息</li>
<li>2 明确每张表中的关键字段(主键，外键，非空)</li>
<li>3 明确数据库中表与表之间的外键关系</li>
<li>4 明确业务中表与表的关系(建立在具体的业务上)</li>
</ul>
<h3 id="模型分析"><a href="#模型分析" class="headerlink" title="模型分析"></a>模型分析</h3><p>以上是本次测试所使用的数据库关系模型：<br><img src="http://om6odecqr.bkt.clouddn.com/%E5%85%B3%E7%B3%BB%E5%9B%BE.png" alt="关系图"></p>
<p>上图可分为两类分析：</p>
<ul>
<li>一 数据库表之间有外键关系的业务关系<ul>
<li>1 user-&gt;orders： 一个用户可以创建多个订单，一对多</li>
<li>1 orders-&gt;user： 一个订单只能由一个用户创建，一对一</li>
<li>2 orders-&gt;orderdetail： 一个订单可以包括多个订单明细。一对多</li>
<li>2 orderdetail-&gt;orders： 一个订单明细只能包括在一个订单中，一对一</li>
<li>3 items-&gt;orderdetail： 一个商品可以包括在多个订单明细，一对多</li>
<li>3 orderdetail-&gt;items： 一个订单明细只对应一个商品学习，一对一</li>
</ul>
</li>
<li>二 数据库表之间没有外键关系的业务关系<ul>
<li>1 orders-&gt;orderedtail-items： 一个订单可以有多个订单明细，一个订单明细对应一个商品，所以一个订单对应多个商品。</li>
<li>1 items-&gt;orderdetail-orders： 一个商品可以对应多个订单明细，一个订单明细对应一个订单，所以一个商品对应多个订单</li>
<li>2 user-&gt;orders-&gt;orderdetail-items： 一个用户有多个订单，一个订单有多个订单明细，一个订单明细对应一个商品，所以一个用户对应多个商品</li>
<li>2 items-&gt;orderdetail-orders-&gt;user： 一个商品对应多个订单明细，一个订单明细对应一个订单，一个订单对应一个用户，所以一个商品对应多个用户</li>
</ul>
</li>
</ul>
<hr>
<h3 id="创建PO类"><a href="#创建PO类" class="headerlink" title="创建PO类"></a>创建PO类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 用户表</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">int</span> id;</div><div class="line">    <span class="keyword">private</span> String username;<span class="comment">// 用户姓名</span></div><div class="line">    <span class="keyword">private</span> String sex;<span class="comment">// 性别</span></div><div class="line">    <span class="keyword">private</span> Date birthday;<span class="comment">// 生日</span></div><div class="line">    <span class="keyword">private</span> String address;<span class="comment">// 地址</span></div><div class="line">    <span class="comment">// 一个用户持有多个订单</span></div><div class="line">    <span class="keyword">private</span> List&lt;Orders&gt; ordersList;</div><div class="line"></div><div class="line"><span class="comment">// 订单表</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Orders</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> Integer id;</div><div class="line">    <span class="keyword">private</span> Integer userId;</div><div class="line">    <span class="keyword">private</span> String number;</div><div class="line">    <span class="keyword">private</span> Date createtime;</div><div class="line">    <span class="keyword">private</span> String note;</div><div class="line">    <span class="comment">// 一个订单属于一个用户</span></div><div class="line">    <span class="keyword">private</span> User user;</div><div class="line">    <span class="comment">// 一个订单对应多个订单明细</span></div><div class="line">    <span class="keyword">private</span> List&lt;Orderdetail&gt; detailList;</div><div class="line"></div><div class="line"><span class="comment">// 订单明细表</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Orderdetail</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> Integer id;</div><div class="line">    <span class="keyword">private</span> Integer ordersId;</div><div class="line">    <span class="keyword">private</span> Integer itemsId;</div><div class="line">    <span class="keyword">private</span> Integer itemsNum;</div><div class="line">    <span class="comment">// 一个订单明细属于一个订单</span></div><div class="line">    <span class="keyword">private</span> Orders orders;</div><div class="line">    <span class="comment">// 一个订单明细属于一个商品</span></div><div class="line">    <span class="keyword">private</span> Items items;</div><div class="line"></div><div class="line"><span class="comment">// 商品表</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Items</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> Integer id;</div><div class="line">    <span class="keyword">private</span> String name;</div><div class="line">    <span class="keyword">private</span> Float price;</div><div class="line">    <span class="keyword">private</span> String pic;</div><div class="line">    <span class="keyword">private</span> Date createtime;</div><div class="line">    <span class="keyword">private</span> String detail;</div><div class="line">    </div><div class="line">    <span class="comment">// 一个商品对应多个订单明细</span></div><div class="line">    <span class="keyword">private</span> List&lt;Orderdetail&gt; detailList;</div></pre></td></tr></table></figure>
<h2 id="一对一查询"><a href="#一对一查询" class="headerlink" title="一对一查询"></a>一对一查询</h2><h3 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">-- 查询订单信息，关联查询用户信息</span></div><div class="line"><span class="keyword">SELECT</span> </div><div class="line">    orders.<span class="string">`id`</span>,</div><div class="line">    orders.<span class="string">`user_id`</span>,</div><div class="line">    orders.<span class="string">`number`</span>,</div><div class="line">    user.<span class="string">`username`</span>,</div><div class="line">    user.<span class="string">`sex`</span></div><div class="line"><span class="keyword">FROM</span></div><div class="line">    orders,</div><div class="line">    <span class="keyword">user</span></div><div class="line"><span class="keyword">WHERE</span></div><div class="line">    orders.<span class="string">`user_id`</span> = user.<span class="string">`id`</span>;</div></pre></td></tr></table></figure>
<h3 id="使用resultType完成功能"><a href="#使用resultType完成功能" class="headerlink" title="使用resultType完成功能"></a>使用resultType完成功能</h3><p>使用resultType来进行一对一结果映射，查询出的列的个数和映射的属性的个数要一致。而且映射的属性要存在与一个大的对象中，它是一种平铺式的映射，即数据库查询出多少条记录，则映射成多少个对象。</p>
<p><strong>1、编写拓展类：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OrdersExt</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="comment">//user.`username`,</span></div><div class="line">    <span class="comment">//user.`sex`</span></div><div class="line">    <span class="keyword">private</span> String username;</div><div class="line">    <span class="keyword">private</span> String sex;</div></pre></td></tr></table></figure></p>
<p><strong>2、编写映射文件</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 定义查询订单列表的SQL片段 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">"select_orders"</span>&gt;</span></div><div class="line">    orders.id,</div><div class="line">    orders.user_id,</div><div class="line">    orders.number,</div><div class="line">    orders.createtime,</div><div class="line">    orders.note</div><div class="line"><span class="tag">&lt;/<span class="name">sql</span>&gt;</span></div><div class="line"><span class="comment">&lt;!-- 定义查询用户列表列名的SQL片段 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">"select_user"</span>&gt;</span></div><div class="line">    user.username,</div><div class="line">    user.sex</div><div class="line"><span class="tag">&lt;/<span class="name">sql</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- 一对一映射之resultType --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findOrdersAndUser"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.po.OrdersExt"</span>&gt;</span></div><div class="line">    SELECT</div><div class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"select_orders"</span>&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span>,</div><div class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"select_user"</span>&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span></div><div class="line">    FROM</div><div class="line">        orders,</div><div class="line">        user</div><div class="line">    WHERE</div><div class="line">        orders.`user_id` = user.`id`;</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>3、Mapper接口：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">OrdersMapper</span> </span>&#123;</div><div class="line">    <span class="comment">//一对一映射之resultType</span></div><div class="line">    <span class="function"><span class="keyword">public</span> List&lt;OrdersExt&gt; <span class="title">findOrdersAndUser</span><span class="params">()</span></span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p><strong>4、测试代码：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testFindOrdersAndUser</span><span class="params">()</span> </span>&#123;</div><div class="line">    SqlSession session = sqlSessionFactory.openSession();</div><div class="line">    </div><div class="line">    OrdersMapper mapper = session.getMapper(OrdersMapper.class);</div><div class="line">    List&lt;OrdersExt&gt; list = mapper.findOrdersAndUser();</div><div class="line">    </div><div class="line">    session.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="使用resultMap"><a href="#使用resultMap" class="headerlink" title="使用resultMap"></a>使用resultMap</h3><p>使用resultMap来进行一对一结果映射，它是将关联对象添加到主信息的对象中，具体说是对象嵌套对象的一种映射方式。</p>
<p><strong>所以需要保证：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// Orders类中包含以下字段：</span></div><div class="line"><span class="comment">// 否则抛出异常：There is no getter for property named 'user' in 'class cn.sqdyy.mybatis.po.OrdersExt'</span></div><div class="line"><span class="keyword">private</span> User user;</div></pre></td></tr></table></figure></p>
<p><strong>映射文件：</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 定义OrdersAndUserRstMap --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"cn.sqdyy.mybatis.po.OrdersExt"</span> <span class="attr">id</span>=<span class="string">"OrdersAndUserRstMap"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"user_id"</span> <span class="attr">property</span>=<span class="string">"userId"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"number"</span> <span class="attr">property</span>=<span class="string">"number"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"createtime"</span> <span class="attr">property</span>=<span class="string">"createtime"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"note"</span> <span class="attr">property</span>=<span class="string">"note"</span>/&gt;</span></div><div class="line">    </div><div class="line">    <span class="comment">&lt;!-- 映射一对一关联关系的用户对象 --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- property：指定关联对象要映射到Orders的哪个属性上 --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- javaType：指定关联对象所要映射的java类型 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">"user"</span> <span class="attr">javaType</span>=<span class="string">"cn.sqdyy.mybatis.po.User"</span>&gt;</span></div><div class="line">        <span class="comment">&lt;!-- id标签：指定关联对象结果集的唯一标识，很重要！不写不会报错，但会影响性能！！ --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"user_id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"username"</span> <span class="attr">property</span>=<span class="string">"username"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"sex"</span> <span class="attr">property</span>=<span class="string">"sex"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">association</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- 一对一映射之resultMap --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findOrdersAndUserRstMap"</span> <span class="attr">resultMap</span>=<span class="string">"OrdersAndUserRstMap"</span>&gt;</span></div><div class="line">    SELECT</div><div class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"select_orders"</span>&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span>,</div><div class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"select_user"</span>&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span></div><div class="line">    FROM</div><div class="line">        orders,</div><div class="line">        user</div><div class="line">    WHERE</div><div class="line">        orders.`user_id` = user.`id`;</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>Mapper接口：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//一对一映射之resultMap</span></div><div class="line"><span class="function"><span class="keyword">public</span> List&lt;OrdersExt&gt; <span class="title">findOrdersAndUserRstMap</span><span class="params">()</span></span>;</div></pre></td></tr></table></figure></p>
<p><strong>测试代码：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testFindOrdersAndUserRstMap</span><span class="params">()</span> </span>&#123;</div><div class="line">    SqlSession session = sqlSessionFactory.openSession();</div><div class="line"></div><div class="line">    OrdersMapper mapper = session.getMapper(OrdersMapper.class);</div><div class="line">    List&lt;OrdersExt&gt; list = mapper.findOrdersAndUserRstMap();</div><div class="line"></div><div class="line">    session.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="一对一小结"><a href="#一对一小结" class="headerlink" title="一对一小结"></a>一对一小结</h3><p><strong>resultType：</strong>使用resultType实现较为简单，如果POJO中没有包括查询出来的列名，需要增加列名对应的属性，即可完成映射，如果没有查询结果的特殊要求建议使用resultType。</p>
<p><strong>resultMap：</strong>需要单独定义resultMap(这就很麻烦了)，如果对查询结果<strong>有特殊要求</strong>，使用resultMap可以完成将关联查询映射到POJO的对象属性中。</p>
<p><strong>所谓特殊要求即：resultMap可以实现延迟加载(嵌套式)，而resultType无法实现延迟加载(平铺式)</strong></p>
<hr>
<h2 id="一对多查询"><a href="#一对多查询" class="headerlink" title="一对多查询"></a>一对多查询</h2><p>一对多查询和一对一查询的配置基本类似。只是如果使用resultMap的话，映射一对多关系要使用collection标签。</p>
<h3 id="需求-1"><a href="#需求-1" class="headerlink" title="需求"></a>需求</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">-- 查询订单信息，关联查询订单明细即用户信息</span></div><div class="line"><span class="keyword">SELECT</span> </div><div class="line">    orders.<span class="string">`id`</span>,</div><div class="line">    orders.<span class="string">`user_id`</span>,</div><div class="line">    orders.<span class="string">`number`</span>,</div><div class="line">    user.<span class="string">`username`</span>,</div><div class="line">    user.<span class="string">`sex`</span>,</div><div class="line">    orderdetail.<span class="string">`id`</span> detailId, </div><div class="line">    orderdetail.<span class="string">`items_id`</span>,</div><div class="line">    orderdetail.<span class="string">`items_num`</span></div><div class="line"><span class="keyword">FROM</span></div><div class="line">    orders,</div><div class="line">    <span class="keyword">user</span>,</div><div class="line">    orderdetail</div><div class="line"><span class="keyword">WHERE</span></div><div class="line">    orders.<span class="string">`user_id`</span> = user.<span class="string">`id`</span></div><div class="line">    <span class="keyword">AND</span> orders.<span class="string">`id`</span> = orderdetail.<span class="string">`orders_id`</span>;</div></pre></td></tr></table></figure>
<p><strong>如果使用resultType将上边的查询结果映射到POJO中，订单信息将会重复：</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">id, user_id, number, username, sex, detailId, items_id, items_num</div><div class="line">&apos;3&apos;, &apos;1&apos;, &apos;1000010&apos;, &apos;王五&apos;, &apos;2&apos;, &apos;1&apos;, &apos;1&apos;, &apos;1&apos;</div><div class="line">&apos;3&apos;, &apos;1&apos;, &apos;1000010&apos;, &apos;王五&apos;, &apos;2&apos;, &apos;2&apos;, &apos;2&apos;, &apos;3&apos;</div><div class="line">&apos;4&apos;, &apos;1&apos;, &apos;1000011&apos;, &apos;王五&apos;, &apos;2&apos;, &apos;3&apos;, &apos;3&apos;, &apos;4&apos;</div><div class="line">&apos;4&apos;, &apos;1&apos;, &apos;1000011&apos;, &apos;王五&apos;, &apos;2&apos;, &apos;4&apos;, &apos;2&apos;, &apos;3&apos;</div></pre></td></tr></table></figure></p>
<h3 id="使用resultMap-1"><a href="#使用resultMap-1" class="headerlink" title="使用resultMap"></a>使用resultMap</h3><p><strong>如果要避免对orders映射不出现重复记录应使用resultMap，必须在orders.java或ordersext.java中添加：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 一个订单对应多个订单明细</span></div><div class="line"><span class="keyword">private</span> List&lt;Orderdetail&gt; detailList;</div></pre></td></tr></table></figure></p>
<p><strong>编写映射文件：</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 定义OrdersAndOrderdetailRstMap --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 使用extends：继承已有的ResultMap，值为继承的ResultMap的唯一标识 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 继承的resultMap的type和它本身的type要保存一致 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"cn.sqdyy.mybatis.po.OrdersExt"</span> <span class="attr">id</span>=<span class="string">"OrdersAndOrderdetailRstMap"</span> <span class="attr">extends</span>=<span class="string">"OrdersAndUserRstMap"</span>&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 映射一对多关系 一个订单有多个订单明细 --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- collection标签：定义一个一对多关系 --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- ofType：指定该集合参数所映射的类型 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"detailList"</span> <span class="attr">ofType</span>=<span class="string">"cn.sqdyy.mybatis.po.Orderdetail"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"detail_id"</span> <span class="attr">property</span>=<span class="string">"id"</span> /&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"items_id"</span> <span class="attr">property</span>=<span class="string">"itemsId"</span> /&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"items_num"</span> <span class="attr">property</span>=<span class="string">"itemsNum"</span> /&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- 一对多：查询订单信息，包括用户名称，用户地址，订单商品信息(嵌套结果) --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findOrdersAndOrderdetailRstMap"</span> <span class="attr">resultMap</span>=<span class="string">"OrdersAndOrderdetailRstMap"</span>&gt;</span></div><div class="line">    SELECT</div><div class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"select_orders"</span>&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span></div><div class="line">        ,</div><div class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"select_user"</span>&gt;</span><span class="tag">&lt;/<span class="name">include</span>&gt;</span></div><div class="line">        ,</div><div class="line">        orderdetail.`id` detailId,</div><div class="line">        orderdetail.`items_id`,</div><div class="line">        orderdetail.`items_num`</div><div class="line">    FROM</div><div class="line">        orders,</div><div class="line">        user,</div><div class="line">        orderdetail</div><div class="line">    WHERE</div><div class="line">        orders.`user_id` = user.`id`</div><div class="line">        AND</div><div class="line">        orders.`id` = orderdetail.`orders_id`;</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>编写mapper接口：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//一对多映射之resultMap</span></div><div class="line"><span class="function"><span class="keyword">public</span> List&lt;Orders&gt; <span class="title">findOrdersAndOrderdetailRstMap</span><span class="params">()</span></span>;</div></pre></td></tr></table></figure></p>
<p><strong>编写测试：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testFindOrdersAndOrderdetailRstMap</span><span class="params">()</span> </span>&#123;</div><div class="line">    SqlSession session = sqlSessionFactory.openSession();</div><div class="line"></div><div class="line">    OrdersMapper mapper = session.getMapper(OrdersMapper.class);</div><div class="line">    List&lt;Orders&gt; list = mapper.findOrdersAndOrderdetailRstMap();</div><div class="line"></div><div class="line">    session.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="多对多查询"><a href="#多对多查询" class="headerlink" title="多对多查询"></a>多对多查询</h2><p>多对多映射时一对多映射的特例</p>
<h3 id="需求-2"><a href="#需求-2" class="headerlink" title="需求"></a>需求</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="comment">-- 查询用户信息及用户购买的商品信息，要求将关联信息映射到主POJO的POJO属性中</span></div><div class="line"><span class="keyword">SELECT</span> </div><div class="line">    orders.<span class="string">`id`</span>,</div><div class="line">    orders.<span class="string">`user_id`</span>,</div><div class="line">    orders.<span class="string">`number`</span>,</div><div class="line">    user.<span class="string">`username`</span>,</div><div class="line">    user.<span class="string">`sex`</span>,</div><div class="line">    orderdetail.<span class="string">`id`</span> detailId, </div><div class="line">    orderdetail.<span class="string">`items_id`</span>,</div><div class="line">    orderdetail.<span class="string">`items_num`</span>,</div><div class="line">    items.<span class="string">`name`</span>,</div><div class="line">    items.<span class="string">`price`</span></div><div class="line"><span class="keyword">FROM</span></div><div class="line">    orders,</div><div class="line">    <span class="keyword">user</span>,</div><div class="line">    orderdetail,</div><div class="line">    items</div><div class="line"><span class="keyword">WHERE</span></div><div class="line">    orders.<span class="string">`user_id`</span> = user.<span class="string">`id`</span></div><div class="line">	<span class="keyword">AND</span> orders.<span class="string">`id`</span> = orderdetail.<span class="string">`orders_id`</span></div><div class="line">    <span class="keyword">AND</span> orderdetail.<span class="string">`items_id`</span> = items.<span class="string">`id`</span>;</div></pre></td></tr></table></figure>
<p><strong>此次主表是User，确保User.java类包含：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 一个用户持有多个订单</span></div><div class="line"><span class="keyword">private</span> List&lt;Orders&gt; orders;</div></pre></td></tr></table></figure></p>
<p><strong>确保Orders.java类包含：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 一个订单对应多个订单明细</span></div><div class="line"><span class="keyword">private</span> List&lt;Orderdetail&gt; detailList;</div></pre></td></tr></table></figure></p>
<p><strong>确保Orderdetail.java类包含：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 一个订单明细属于一个商品</span></div><div class="line"><span class="keyword">private</span> Items items;</div></pre></td></tr></table></figure></p>
<p><strong>映射文件：</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- UserAndItemsRstMap --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"cn.sqdyy.mybatis.po.User"</span> <span class="attr">id</span>=<span class="string">"UserAndItemsRstMap"</span>&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 用户信息 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"user_id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"username"</span> <span class="attr">property</span>=<span class="string">"username"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"sex"</span> <span class="attr">property</span>=<span class="string">"sex"</span>/&gt;</span></div><div class="line"></div><div class="line">    <span class="comment">&lt;!-- 订单信息(一个用户有多个订单) --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"ordersList"</span> <span class="attr">ofType</span>=<span class="string">"cn.sqdyy.mybatis.po.Orders"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"user_id"</span> <span class="attr">property</span>=<span class="string">"userId"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"number"</span> <span class="attr">property</span>=<span class="string">"number"</span>/&gt;</span></div><div class="line"></div><div class="line">        <span class="comment">&lt;!-- 订单明细信息(一个订单有多个订单明细) --&gt;</span> </div><div class="line">        <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"detailList"</span> <span class="attr">ofType</span>=<span class="string">"cn.sqdyy.mybatis.po.Orderdetail"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"detail_id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"items_id"</span> <span class="attr">property</span>=<span class="string">"itemsId"</span>/&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"items_num"</span> <span class="attr">property</span>=<span class="string">"itemsNum"</span>/&gt;</span></div><div class="line"></div><div class="line">            <span class="comment">&lt;!-- 商品信息(一个订单明细对应一个商品) --&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">"items"</span> <span class="attr">javaType</span>=<span class="string">"cn.sqdyy.mybatis.po.Items"</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"items_id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"items_name"</span> <span class="attr">property</span>=<span class="string">"name"</span>/&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"items_price"</span> <span class="attr">property</span>=<span class="string">"price"</span>/&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">association</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- 查询用户及用户购买商品信息(多对多resultMap) --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserAndItemsRstMap"</span> <span class="attr">resultMap</span>=<span class="string">"UserAndItemsRstMap"</span>&gt;</span></div><div class="line">    SELECT</div><div class="line">        orders.`id`,</div><div class="line">        orders.`user_id`,</div><div class="line">        orders.`number`,</div><div class="line">        user.`username`,</div><div class="line">        user.`sex`,</div><div class="line">        orderdetail.`id` detailId,</div><div class="line">        orderdetail.`items_id`,</div><div class="line">        orderdetail.`items_num`,</div><div class="line">        items.`name` items_name,</div><div class="line">        items.`price` items_price</div><div class="line">    FROM</div><div class="line">        orders,</div><div class="line">        user,</div><div class="line">        orderdetail,</div><div class="line">        items</div><div class="line">    WHERE</div><div class="line">        orders.`user_id` = user.`id`</div><div class="line">        AND orders.`id` = orderdetail.`orders_id`</div><div class="line">        AND orderdetail.`items_id` = items.`id`;</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>Mapper接口：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 多对多映射</span></div><div class="line"><span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">findUserAndItemsRstMap</span><span class="params">()</span></span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p><strong>测试代码：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testFindUserAndItmensRstMap</span><span class="params">()</span> </span>&#123;</div><div class="line">    SqlSession session = sqlSessionFactory.openSession();</div><div class="line"></div><div class="line">    UserMapper mapper = session.getMapper(UserMapper.class);</div><div class="line">    List&lt;User&gt; list = mapper.findUserAndItemsRstMap();</div><div class="line">    System.out.println(list);</div><div class="line">    </div><div class="line">    session.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="延迟加载"><a href="#延迟加载" class="headerlink" title="延迟加载"></a>延迟加载</h2><p>前面提到的resultMap中association和collection标签具有延迟加载功能。延迟加载的意思是说，在关联查询时，利用延迟加载，先加载主信息。需要关联信息时再去按需加载关联信息。这样会大大提高数据库性能，因为查询单表要比关联查询多表速度要快。</p>
<h3 id="如何设置延迟加载"><a href="#如何设置延迟加载" class="headerlink" title="如何设置延迟加载"></a>如何设置延迟加载</h3><p>Mybatis默认是不开启延迟加载功能的(这也是为什么我们之前的代码测试都能直接得到所有记录的原因)，我们需要手动开启。我们需要在SqlMapConfig.xml文件中，在<settings>标签中开启延迟加载功能。<strong>lazyLoadingEnabled</strong>，<strong>aggressiveLazyLoading</strong>。</settings></p>
<table>
<thead>
<tr>
<th style="text-align:left">参数项</th>
<th style="text-align:left">描述</th>
<th style="text-align:left">允许值</th>
<th style="text-align:left">默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">lazyLoadingEnabled</td>
<td style="text-align:left">全局性设置懒加载。如果设为‘false’，则所有相关联的都会被初始化加载</td>
<td style="text-align:left">true/false</td>
<td style="text-align:left">false</td>
</tr>
<tr>
<td style="text-align:left">aggressiveLazyLoading</td>
<td style="text-align:left">当设置为‘true’的时候，懒加载的对象可能被任何懒属性全部加载。否则，每个属性都按需加载。</td>
<td style="text-align:left">true/false</td>
<td style="text-align:left">false</td>
</tr>
</tbody>
</table>
<p><strong>开启延迟加载功能：</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">settings</span>&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 打开延迟加载的开关 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"lazyLoadingEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 将积极加载改为消极加载，即按需加载 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"aggressiveLazyLoading"</span> <span class="attr">value</span>=<span class="string">"false"</span>/&gt;</span>        </div><div class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="使用association进行延迟加载"><a href="#使用association进行延迟加载" class="headerlink" title="使用association进行延迟加载"></a>使用association进行延迟加载</h3><p>查询订单并且关联查询用户信息(对用户信息的加载要求时按需加载)</p>
<p><strong>编写映射文件(需要定义两个mapper的方法对应的statement)：</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 1、只查询订单信息 select * from orders --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 在查询订单的statement中使用association去延迟加载(执行)下边的statement(关联查询用户信息) --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 定义OrdersUserLazyLoadingRstMap --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"cn.sqdyy.mybatis.po.Orders"</span> <span class="attr">id</span>=<span class="string">"OrdersUserLazyLoadingRstMap"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"user_id"</span> <span class="attr">property</span>=<span class="string">"userId"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"number"</span> <span class="attr">property</span>=<span class="string">"number"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"createtime"</span> <span class="attr">property</span>=<span class="string">"createtime"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"note"</span> <span class="attr">property</span>=<span class="string">"note"</span>/&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 延迟加载用户信息 --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- select：指定延迟加载需要执行的statement的id(是根据user_id查询用户信息的statement) --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 我们使用UserMapper.xml中的findUserById完成根据用户Id(user_id)查询用户信息 --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 如果findUserById不在本mapper中，前边需要加namespace --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- column：主信息表中需要关联查询的列，此处时user_id --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">"user"</span> <span class="attr">select</span>=<span class="string">"cn.sqdyy.mybatis.mapper.UserMapper.findUserById"</span> <span class="attr">column</span>=<span class="string">"user_id"</span>&gt;</span><span class="tag">&lt;/<span class="name">association</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- 查询订单信息，延迟加载关联查询的用户信息 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findOrdersUserLazyLoadingRstMap"</span> <span class="attr">resultMap</span>=<span class="string">"OrdersUserLazyLoadingRstMap"</span>&gt;</span></div><div class="line">    SELECT * FROM orders;</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>UserMapper.xml：</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserById"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.po.User"</span>&gt;</span></div><div class="line">    SELECT * FROM user</div><div class="line">    WHERE id = #&#123;id&#125;;</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>编写mapper接口：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// OrdersMapper.java</span></div><div class="line"><span class="function"><span class="keyword">public</span> List&lt;Orders&gt; <span class="title">findOrdersUserLazyLoadingRstMap</span><span class="params">()</span></span>;</div><div class="line"><span class="comment">// UserMapper.java</span></div><div class="line"><span class="function"><span class="keyword">public</span> User <span class="title">findUserById</span><span class="params">(<span class="keyword">int</span> id)</span></span>;</div></pre></td></tr></table></figure></p>
<p><strong>测试代码：</strong>执行mapper方法(findOrdersUserLazyLoadingRstMap)只查询orders信息(单表)，接着我们在程序中去遍历List<orders>，当我们调用getUser方法时，开始进行延迟加载(会去调用UserMapper.xml中的findUserById这个方法)。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testFindOrdersUserLazyLoadingRstMap</span><span class="params">()</span> </span>&#123;</div><div class="line">    SqlSession session = sqlSessionFactory.openSession();</div><div class="line"></div><div class="line">    OrdersMapper mapper = session.getMapper(OrdersMapper.class);</div><div class="line">    List&lt;Orders&gt; list = mapper.findOrdersUserLazyLoadingRstMap();</div><div class="line">    <span class="keyword">for</span> (Orders orders : list) &#123;</div><div class="line">        System.out.println(orders.getUser());</div><div class="line">    &#125;</div><div class="line">    session.close();</div><div class="line">&#125;</div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">...</span></div><div class="line"><span class="comment">DEBUG [main] - ==&gt;  Preparing: SELECT * FROM user WHERE id = ?; </span></div><div class="line"><span class="comment">DEBUG [main] - ==&gt; Parameters: 1(Integer)</span></div><div class="line"><span class="comment">DEBUG [main] - &lt;==      Total: 1</span></div><div class="line"><span class="comment">User [id=1, username=王五, sex=2, birthday=Thu Jul 10 00:00:00 CST 2014, address=null, orders=null]</span></div><div class="line"><span class="comment">DEBUG [main] - ==&gt;  Preparing: SELECT * FROM user WHERE id = ?; </span></div><div class="line"><span class="comment">DEBUG [main] - ==&gt; Parameters: 10(Integer)</span></div><div class="line"><span class="comment">DEBUG [main] - &lt;==      Total: 1</span></div><div class="line"><span class="comment">User [id=10, username=张三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市, orders=null]</span></div><div class="line"><span class="comment">...</span></div><div class="line"><span class="comment">*/</span></div></pre></td></tr></table></figure></orders></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T10:18:16.496Z" itemprop="dateUpdated">2017年12月5日 18:18</time>
</span><br>


        如要转载请注明出处：<a href="/2017/08/14/Mybatis_primary_05/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/SSM/">SSM</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/&title=《Mybatis快速入门五(关联查询映射)》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/&title=《Mybatis快速入门五(关联查询映射)》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Mybatis快速入门五(关联查询映射)》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/08/15/Mybatis_primary_06/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Mybatis快速入门六(一级缓存)</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/08/13/Mybatis_primary_04/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Mybatis快速入门四(映射文件)</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/&title=《Mybatis快速入门五(关联查询映射)》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/&title=《Mybatis快速入门五(关联查询映射)》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Mybatis快速入门五(关联查询映射)》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/08/14/Mybatis_primary_05/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
